package game;

import java.awt.Point;

/**
 *	@(#)LandSystem.java
 *
 *	The <code>LandSystem</code> class represents the game's coordinate system
 *	and provides methods to calculate real positions in the window from given model
 *	points and vice-versa.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2007/9/11
 */
public abstract class LandSystem
{
	/** The length of the unit in x-direction. */
	public static final int UNIT_X = 12;

	/** The length of the unit in y-direction. */
	public static final int UNIT_Y = 21;

	/** The length of a step in x-direction. */
	public static final int DX = 3;

	/** The length of a step in y-direction. */
	public static final int DY = 1;

	/** The offset to the origin in x-direction. */
	public static final int OFFSET_X = 2;

	/** The offset to the origin in y-direction. */
	public static final int OFFSET_Y = 2;

	/**
	 *	Returns the pixel coordinates of the given system point.
	 *	@param point the point
	 *	@return the pixel coordinates
	 */
	public static Point getPixel(Point point) {
		return getPixel(point.x, point.y);
	}

	/**
	 *	Returns the pixel coordinates of the given system point.
	 *	@param x the x-coordinate
	 *	@param y the y-coordinate
	 *	@return the pixel coordinates
	 */
	public static Point getPixel(double x, double y) {
		return new Point((int)Math.round(UNIT_X * x), (int)Math.round(UNIT_Y * y));
	}

	/**
	 *	Returns the corresponding point in the coordinate system.
	 *	@param pixel the pixel coordinates
	 *	@return the corresponding system point
	 */
	public static Point getPoint(Point pixel) {
		return getPoint(pixel.x, pixel.y);
	}

	/**
	 *	Returns the corresponding point in the coordinate system.
	 *	@param x the x-coordinate of the given pixel
	 *	@param y the y-coordinate of the given pixel
	 *	@return the corresponding system point
	 */
	public static Point getPoint(double x, double y) {
		x = (x / UNIT_X - OFFSET_X) / DX;
		y = (y / UNIT_Y - OFFSET_Y) / DY;
		int X = (int)Math.round(x);
		int Y = (int)Math.round((x + y) / 2);
		return new Point(OFFSET_X + DX * X, OFFSET_Y + DY * (2 * Y - X));
	}
}